package org.particlethink.mapper;

import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;
import org.particlethink.dto.DeliverStatusAndCount;
import org.particlethink.entry.DeliverStatus;
import org.particlethink.model.Deliver;
import tk.mybatis.mapper.common.Mapper;

import java.util.Date;
import java.util.List;

@org.apache.ibatis.annotations.Mapper
public interface DeliverMapper extends Mapper<Deliver> {
    @Select("select * from delivers where order_id =  #{orderId}")
    List<Deliver> selectByOrderId(@Param("orderId") int orderId);

    @Select("select * from delivers where order_id=#{orderId} and status = #{status.code}")
    List<Deliver> selectByOrderAndStatus(@Param("orderId") int orderId, @Param("status") DeliverStatus status);

    List<Deliver> selectByOrderAndStatuses(@Param("orderId") Integer orderId, @Param("statuses") List<DeliverStatus> status);


    @Select("select * from delivers where supplier_id=#{supplierId} and status = #{status.code}")
    List<Deliver> selectBySupplierAndStatus(@Param("supplierId") Integer supplierId, @Param("status") DeliverStatus status);

    @Select("select * from delivers where supplier_id=#{supplierId}")
    List<Deliver> selectBySupplier(@Param("supplierId") Integer supplierId);

    @Select("select * from delivers where status=#{status.code}")
    List<Deliver> selectByStatus(@Param("status") DeliverStatus status);

    @Select("select * from delivers where project_id=#{projectId}")
    List<Deliver> selectByProject(@Param("projectId") Integer projectId);

    @Select("select * from delivers where project_id=#{projectId} and status=#{status.code}")
    List<Deliver> selectByProjectAndStatus(@Param("projectId") Integer projectId, @Param("status") DeliverStatus status);

    @Update({"<script>",
            "update delivers",
            "set status = #{status.code}",
            "where id in ",
            "<foreach item='item' index='index' collection='deliverIDs' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"})
    void UpdateStatusByIDs(@Param("deliverIDs") List<Integer> deliverIDs, @Param("status") DeliverStatus status);


    List<Deliver> selectByOrderIdsAndStatus(@Param("orderIds") List<Integer> orderIds, @Param("status") DeliverStatus status);

    List<Deliver> selectByOrderIdsAndStatuses(@Param("orderIds") List<Integer> orderIds, @Param("statuses") List<DeliverStatus> status);

    @Select("select * from delivers where bill_id=#{billId}")
    List<Deliver> selectByBillId(@Param("billId") Integer billId);


    @Update({"<script>",
            "update delivers",
            "set bill_id = #{billId},",
            "bill_at = #{billAt}",
            "where id in ",
            "<foreach item='item' index='index' collection='deliverIDs' open='(' separator=',' close=')'>",
            "#{item}",
            "</foreach>",
            "</script>"})
    void UpdateBillIdAndAtByIDs(@Param("deliverIDs") List<Integer> deliverIds, @Param("billId") int billId, @Param("billAt") Date billAt);

    @Select("select * from delivers where bill_id=#{billId}")
    List<Deliver> selectByBillID(@Param("billId") int billId);


    List<Deliver> selectByNo(@Param("no") String no, @Param("status") DeliverStatus status);

    List<Deliver> queryDelivers(
            @Param("ids") List<Integer> ids,
            @Param("projectIds") List<Integer> projectIds,
            @Param("supplierIds") List<Integer> supplierIds,
            @Param("no") String no,
            @Param("orderNo") String orderNo,
            @Param("categoryId") Integer categoryId,
            @Param("carNumber") String carNumber,
            @Param("supplierUserIds") List<Integer> supplierUserIds, // 发货人
            @Param("acceptanceUserIds") List<Integer> acceptanceUserIds, // 验收人
            @Param("acceptanceStart") Date acceptanceStart,
            @Param("acceptanceEnd") Date acceptanceEnd,
            @Param("supplierId") Integer supplierId,
            @Param("status") DeliverStatus status,
            @Param("shouldBilled") Boolean shouldBilled,
            @Param("submitStart") Date submitStart,
            @Param("submitEnd") Date submitEnd
    );

    Integer countByOrderId(@Param("orderId") Integer orderId);

    List<DeliverStatusAndCount> countByStatus(@Param("supplierIds") List<Integer> supplierIds, @Param("projectIds") List<Integer> projectIds);

}
